package com.lmabbe.common.global.context.session;

import com.lmabbe.common.global.constant.GlobalConstant;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * oauth的userDetails 的实现
 *
 * @author lmabbe
 */
@Data
public class JwtUser implements UserDetails {

    private LoginUser loginUser;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return loginUser.getAuthorities();
    }

    @Override
    public String getPassword() {
        return loginUser.getUserPassword();
    }

    @Override
    public String getUsername() {
        return loginUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return loginUser.getPasswordErrorNum() <= 5;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return loginUser.getValid().equals(GlobalConstant.IS_VALID.YES);
    }
}
